package com.example.jack.community.view.home;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.jack.community.R;
import com.example.jack.community.data.HomePhotoConfig;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 *
 * 嵌套了viewpager的图片浏览
 */

public class HomeItemImagesDialog extends Dialog {

    private View mView ;
    private Context mContext;
    private HomeItemImagesViewPager mViewPager;
    private TextView mIndexText;
    private List<String> mImgUrls;
    private List<String> mTitles;
    private List<View> mViews;
    private HomeItemImagesAdapter mAdapter;
    private int Touchposition;

    public HomeItemImagesDialog(@NonNull Context context, List<String> imgUrls, int pos) {
        super(context, R.style.transparentBgDialog);
        this.mContext = context; //上下文
        this.mImgUrls = imgUrls; //整组图片地址
        Touchposition = pos;   //当前点击下的pos
        initView();
        initData();
    }

    private void initView() {
        mView = View.inflate(mContext, R.layout.home_dialog_images_brower, null);
        mViewPager = (HomeItemImagesViewPager) mView.findViewById(R.id.home_item_vp_images);
        mIndexText = (TextView) mView.findViewById(R.id.home_item_tv_image_index);

        mTitles = new ArrayList<>();
        mViews = new ArrayList<>();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mView);
        //拿到屏幕的宽高然后在将内容居中
        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
        wl.height = HomePhotoConfig.EXACT_SCREEN_HEIGHT;
        wl.width = HomePhotoConfig.EXACT_SCREEN_WIDTH;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }

    private void initData() {
        PhotoViewAttacher.OnPhotoTapListener listener = new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                dismiss();  //图片点击一下就退出放大
            }
        };

        for ( int i = 0; i < mImgUrls.size(); i++) {

            final PhotoView photoView = new PhotoView(mContext);
            //设置布局参数然后设置到图片上去让它放大
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            photoView.setLayoutParams(layoutParams);
            //设置监听器
            photoView.setOnPhotoTapListener(listener);
            //设置glide配置，ps:glide到时候抽取
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                  ;
            Glide.with(mContext)
                    .load(mImgUrls.get(i))
                    .apply(options)
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                            photoView.setImageDrawable(resource);
                        }
                    });
            //存入集合
            mViews.add(photoView);
            mTitles.add(i + "");
        }

        mAdapter = new HomeItemImagesAdapter(mViews, mTitles);
         mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(Touchposition);//点击下去的位置设为初始位置
        mIndexText.setText(1+Touchposition + "/" + mImgUrls.size());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndexText.setText(position+ 1 + "/" + mImgUrls.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
